hast-util-select
hast utility with equivalents querySelector
, querySelectorAll
,
and matches
.
One notable difference between DOM and hast is that DOM nodes have references
to their parents, meaning that document.body.matches(':last-child')
can
be evaluated.
This information is not stored in hast, so selectors like that don’t work.
View the list of supported selectors »
Install
npm:
npm install hast-util-select
API
select.matches(selector, node[, space])
Check that the given node
matches selector
.
Returns boolean, whether the node matches or not.
This only checks the element itself, not the surrounding tree.
Thus, nesting in selectors is not supported (p b
, p > b
), neither are
selectors like :first-child
, etc.
This only checks that the given element matches the selector.
Use
var h = require('hastscript')
var matches = require('hast-util-select').matches
matches('b, i', h('b'))
matches(':any-link', h('a'))
matches(':any-link', h('a', {href: '#'}))
matches('.classy', h('a', {className: ['classy']}))
matches('#id', h('a', {id: 'id'}))
matches('[lang|=en]', h('a', {lang: 'en'}))
matches('[lang|=en]', h('a', {lang: 'en-GB'}))
Parameters
selector
(string
)
— CSS selectors (,
is also supported)node
(Node
)
— Thing to check, could be anything, but should be an elementspace
(enum, 'svg'
or 'html'
, default: 'html'
)
— Which space the node exists in
Returns
boolean
— Whether the node matches the selector.
select.select(selector, tree[, space])
Select the first node
matching selector
in the given tree
(could be the
tree itself).
Searches the tree in preorder.
Use
var h = require('hastscript')
var select = require('hast-util-select').select
console.log(
select(
'h1 ~ :nth-child(even)',
h('section', [
h('p', 'Alpha'),
h('p', 'Bravo'),
h('h1', 'Charlie'),
h('p', 'Delta'),
h('p', 'Echo')
])
)
)
Yields:
{ type: 'element',
tagName: 'p',
properties: {},
children: [ { type: 'text', value: 'Delta' } ] }
Parameters
selector
(string
) — CSS selectors (,
is also supported)tree
(Node
) — Tree to searchspace
(enum, 'svg'
or 'html'
, default: 'html'
)
— Which space the tree exists in
Returns
Element?
— The found element, if any.
select.selectAll(selector, tree[, space])
Select all nodes matching selector
in the given tree
(could include the tree
itself).
Searches the tree in preorder.
Use
var h = require('hastscript')
var selectAll = require('hast-util-select').selectAll
console.log(
selectAll(
'h1 ~ :nth-child(even)',
h('section', [
h('p', 'Alpha'),
h('p', 'Bravo'),
h('h1', 'Charlie'),
h('p', 'Delta'),
h('p', 'Echo'),
h('p', 'Foxtrot'),
h('p', 'Golf')
])
)
)
Yields:
[ { type: 'element',
tagName: 'p',
properties: {},
children: [ { type: 'text', value: 'Delta' } ] },
{ type: 'element',
tagName: 'p',
properties: {},
children: [ { type: 'text', value: 'Foxtrot' } ] } ]
Parameters
selector
(string
) — CSS selectors (,
is also supported)tree
(Node
) — Tree to searchspace
(enum, 'svg'
or 'html'
, default: 'html'
)
— Which space the tree exists in
Returns
Array.<Element>
— All found elements, if any.
Support
Unsupported
Notes
- * — Not supported in
matches
- † — Needs a user, browser, interactivity, or scripting to make sense
- ‡ — Not supported by the underlying algorithm
- § — Not very interested in writing / including the code for this
- ‖ — Too new, the spec is still changing
Security
hast-util-select
does not change the syntax tree so there are no openings for
cross-site scripting (XSS) attacks.
Related
Contribute
See contributing.md
in syntax-tree/.github
for ways to get
started.
See support.md
for ways to get help.
This project has a code of conduct.
By interacting with this repository, organization, or community you agree to
abide by its terms.
License
MIT © Titus Wormer